//
//  ViewController.m
//  TestCLTagView
//
//  Created by zhuzhijia on 2018/1/31.
//  Copyright © 2018年 zhuzhijia. All rights reserved.
//

#import "ViewController.h"
#import "CLTagsModel.h"
#import "CLRecentTagView.h"
#import "CLTools.h"
#import "TagViewTool.h"
@interface ViewController (){
    CLRecentTagView *_recentTagView;
    NSArray *_constraintsArr ;
    BOOL _isFlag;
    
    UIView *_superView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor grayColor];
    [self loadData];
    
    //[self showUI];
    
    //[self testAddsubViewByFrame];
    [self testAddsubViewByAutoLayout];
    
    // nav的属性，根据自己的需求更改
#warning properties of navgitonController, you can reset them for your app
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:cl_colorWithHex(0x66d547) forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveItemClick2:) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn sizeToFit];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    
}

-(void)testAddsubViewByFrame{//子视图 会 贴在父视图的上面 正常展示
    
    UIView *superView = [[UIView alloc] init];
    [self.view addSubview:superView];
    superView.frame = CGRectMake(0, 0, 0, 0);
    superView.backgroundColor = [UIColor redColor];
    
    UIView *subView = [[UIView alloc] init];
    [superView addSubview:subView];
    subView.frame = CGRectMake(0, 0, 200, 200);
    subView.backgroundColor = [UIColor yellowColor];
    
}

-(void)testAddsubViewByAutoLayout{//子视图 会 贴在父视图的上面 正常展示
    
    UIView *superView = [[UIView alloc] init];
    _superView = superView;
    [self.view addSubview:superView];
    [superView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(100);
        make.width.height.mas_equalTo(100);
    }];
    superView.backgroundColor = [UIColor redColor];
    
    UIView *subView = [[UIView alloc] init];
    [superView addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(superView);
        make.width.height.mas_equalTo(superView);
    }];
    subView.backgroundColor = [UIColor yellowColor];
    
    //对于scrollview而言 画框是肉眼可以的UI 而画布是 只有处于画框之中 才是可以的，不在画框之中则是不可见的。这个可以特性可以利用来实现一些特殊效果
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.alwaysBounceVertical = YES;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [subView addSubview:scrollView];
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.contentSize = CGSizeZero;
    
    UIView *subView1 = [[UIView alloc] init];
    [scrollView addSubview:subView1];
    subView1.frame = CGRectMake(0, 0, 100, 100);
    subView1.backgroundColor = [UIColor greenColor];
    
}


- (void)viewDidLayoutSubviews{
    NSLog(@"_recentTagView frame1:%@",NSStringFromCGRect(_recentTagView.frame));
    
    NSLog(@"_superView frame1:%@",NSStringFromCGRect(_superView.frame));
}

-(void)viewDidAppear:(BOOL)animated{
    
    NSLog(@"_recentTagView frame2:%@",NSStringFromCGRect(_recentTagView.frame));
    
    NSLog(@"_superView frame1:%@",NSStringFromCGRect(_superView.frame));
    
    //_recentTagView.frame = CGRectMake(100, 100, 100, 100);
//    [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
//        _recentTagView.frame = CGRectMake(100, 100, 100, 100);
//    } completion:^(BOOL finished) {
//
//    }];
    
    //_superView.frame = CGRectMake(100, 100, 300, 300);//可以修改的
    
}

-(void)loadData{
    
    self.cornerRadius = 33.0 / 2.0;
    
    CLTagsModel *model = [[CLTagsModel alloc] init];
    model.title = @"第一印象";
    model.tagsArray = @[@"牛B", @"霸气", @"杜甫", @"李白", @"asdadadasd", @"1231313131", @"sdksalkjfalkjfaj", @"zvkzknmncmalkjdsfkljaskldf"];
    self.tagsModelArray = @[model];
    //self.tagsModelArray = @[model, model1, model2];  // 传入多个模型，显示多个标签组
}

- (void)showUI {
    [CLTools sharedTools].cornerRadius = self.cornerRadius;
    CGFloat originalY = 0;
    if (self.navigationController) {
        originalY = 64;
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _recentTagView = [[CLRecentTagView alloc] init];
    [self.view addSubview:_recentTagView];
    
    _recentTagView.translatesAutoresizingMaskIntoConstraints = NO;
    
    //VFL设置约束
#if 0
    NSArray *constraintsArr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-64-[_recentTagView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_recentTagView)];
    _constraintsArr = constraintsArr;
    [self.view addConstraints:constraintsArr];
    //[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-64-[_recentTagView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_recentTagView)]];
    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_recentTagView]-0-|" options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight metrics:nil views:NSDictionaryOfVariableBindings(_recentTagView)]];
#elif 1
    //masonry设置约束 -z
    [_recentTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(64);
        make.bottom.equalTo(self.view).offset(0);
    }];
#endif
    
    _recentTagView.tagsModel = self.tagsModelArray;

    self.navigationController.navigationBar.barTintColor = cl_colorWithHex(0x3a393d);
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"标签";
    
//    [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionLayoutSubviews animations:^{
//        [_recentTagView layoutIfNeeded];
//    } completion:^(BOOL finished) {
//
//    }];
}

// 点击保存，获取输入的标签
- (void)saveItemClick:(UIButton *)sender {
    
    [self.view removeConstraints:_constraintsArr];
    if (_isFlag) {
        
#if 0
        NSArray *newConstraintsArr =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-64-[_recentTagView(==height)]-|" options:0 metrics:@{@"height": @(0)} views:NSDictionaryOfVariableBindings(_recentTagView)];
        _constraintsArr = newConstraintsArr;
        [self.view addConstraints:newConstraintsArr];
#elif 1
        [_recentTagView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-(self.view.frame.size.height - 64));
        }];
        [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
#endif
    }else{
#if 0
    NSArray *constraintsArr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-64-[_recentTagView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_recentTagView)];
    _constraintsArr = constraintsArr;
    [self.view addConstraints:constraintsArr];
#elif 1
    [_recentTagView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
    }];
    [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
#endif
    }
    _isFlag = !_isFlag;
    
    //[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_recentTagView(==height)]|" options:0 metrics:@{@"height": @(0)} views:NSDictionaryOfVariableBindings(_recentTagView)]];
    [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
        [_recentTagView layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)saveItemClick1:(UIButton *)sender {
    
        [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
            _recentTagView.frame = CGRectMake(100, 100, 200, 200);
        } completion:^(BOOL finished) {
    
        }];
}


- (void)saveItemClick2:(UIButton *)sender {
    
    NSLog(@"saveItemClick2");
    [_superView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    [UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
         [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
